﻿
<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>计划占用(无码原料)</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link href="~/js/layui/css/layui.css" rel="stylesheet" />
    <link href="~/X-admin/css/font.css" rel="stylesheet" />
    @* <script src="~/js/layui/layui.js"></script> *@
    <script src="~/X-admin/lib/layui/layui.js"></script>
    <link href="~/X-admin/css/xadmin.css" rel="stylesheet" />
    <script src="~/X-admin/js/jquery.min.js"></script>
    <script src="~/X-admin/js/xadmin.js"></script>
    <script src="~/js/framework/sysframework.js"></script>
    <script src="~/js/sysweb/planjs.js"></script>
    <script src="~/Scripts/formSelect/multiSelect.js"></script>
    <script src="~/js/framework/commonjs.js"></script>
    <script type="text/html" id="barDemo">
        <a title="删除" href="javascript:;" lay-event="del" id="frmDel"><i class="layui-icon">&#xe640;</i></a>
    </script>
</head>
<body>
    <ul lay-bar="disabled" class="mWebBanner">
        <li class="layui-nav-item" style="float:left;">
            <button type="button" name="btn_update" onclick="confirmZy()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#xe697;</i>确认占用
            </button>
        </li>
        <li class="layui-nav-item" style="float:left;margin-left:3%">
            <button type="button" name="btn_update" onclick="applyPurchase()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#xe698;</i>申请采购
            </button>
        </li>
        <li class="layui-nav-item" style="float:left;margin-left:3%">
            <button type="button" onclick="closeWin()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#x1007;</i>关闭页面
            </button>
        </li>
    </ul>
    <div class="layui-card" style="margin-top:1%;padding-top:2%" >
        <form id="pForm" name="pForm" class="layui-form layui-col-space5" style="margin-left:15px">
            <div class="layui-form-item">
                <div class="layui-input-group" style="float:left;">
                    <div class="layui-input-prefix">
                        计划号
                    </div>
                    <input type="text" id="plCode" name="plCode" class="layui-input"  readonly />
                </div>
                <div class="layui-input-group" style="float:left;margin-left:2%">
                    <div class="layui-input-prefix">
                        客 户
                    </div>
                    <input type="text" id="cuName" name="cuName" class="layui-input" style="width:130%" readonly />
                </div>
                <div class="layui-input-group" style="float:left;margin-left:7%">
                    <div class="layui-input-prefix">
                        计划存货
                    </div>
                    <input type="text" id="plProName" name="plProName" class="layui-input" style="width:230%" readonly />
                </div>

              
            </div>
          
        </form>

        <form class="layui-form layui-col-space5" style="margin-left:2.5%" name="qForm" id="qForm">
            <input type="hidden" id="planNum" name="planNum">
            <input type="hidden" id="proCode" name="proCode">
            <input type="hidden" id="plCode" name="plCode">
            <input type="hidden" id="numType" name="numType">
            <input type="hidden" id="gxNames" name="gxNames">
            <div class="layui-inline layui-show-xs-block">
                <input type="text" name="mProCode" placeholder="原辅料编码" autocomplete="off" class="layui-input" id="mProCode" onchange="loadBOM()">
            </div>
            <div class="layui-inline layui-show-xs-block">
                <input type="text" name="proName" placeholder="原辅料名称" autocomplete="off" class="layui-input" id="proName" onchange="loadBOM()">
            </div>
            <div class="layui-inline layui-show-xs-block">
                <input type="text" name="spec" placeholder="原辅料规格" autocomplete="off" class="layui-input" id="spec" onchange="loadBOM()">
            </div>
        </form>

        <form class="layui-form layui-col-space5" style="margin-left:2.5%;margin-top:1%" name="dForm" id="dForm">
            <div class="layui-inline layui-show-xs-block">
                <div class="layui-input-group" style="float:left;">
                    <div class="layui-input-prefix">
                        占用工序
                    </div>
                    <input type="hidden" name="gxCodes" id="gxCodes">
                    <select multiple="multiple" lay-filter="multiple" id="gxCode" name="gxCode"></select>
                </div>
               
            </div>
        </form>
        
        <div class="layui-card-body layui-table-body layui-table-main" id="layui-table-main">
            <table class="layui-hide" id="list" lay-filter="list"></table>
        </div>
        <div class="layui-card-body layui-table-body" style="margin-top:1%" >
            <table class="layui-hide" id="cglist" lay-filter="cglist"></table>
        </div>
    </div>

</body>
</html>
<script>
    // var webCode = "";
    var proCode = "0";
    var plCode = "", planNum = "", numType = "", plProName = "", plSpec = "", cuName = "";

    $(function () {
        proCode = getUrlVariable("proCode");
        plCode = getUrlVariable("plCode");
        planNum = getUrlVariable("planNum");
        numType = getUrlVariable("numType");
        plProName = getUrlVariable("plProName");
        plSpec = getUrlVariable("plSpec");
        cuName = getUrlVariable("cuName");
        $("input[ name='plCode']").val(plCode);
        $("input[ name='planNum']").val(planNum);
        $("input[ name='proCode']").val(proCode);
        $("input[ name='numType']").val(numType);
        $("input[ name='plProName']").val(plProName + " / " + plSpec + " / " + planNum);
        $("input[ name='plSpec']").val(plSpec);
        $("input[ name='cuName']").val(cuName);
        loadProducts();
        getGxData("gxCode", "");
        loadBOM();
        loadPurchaseList();
    })

    //将采购数量存到临时表里
    function loadProducts() {
        $.ajax({
            url: "../Basic/GetProductByCode",
            data: { proCode: proCode },
            type: "post",
            success: function (rsl) {
                if (rsl.result) {
                    let uName = rsl.data[0].unitName;
                    var pInfo = $("input[ name='plProName']").val() + " " + uName;
                    $("input[ name='plProName']").val(pInfo);
                } 
            }
        });
    }

    var dUrl = "../Plan/getDataList?dataType=mstock&condition=";
    var cgUrl = "../Plan/getDataList?dataType=cgList&condition=";
    // var cData = new Array();
    
    /*
    *加载存货bom
    * */
    function loadBOM() {
        var conditon = JSON.stringify(FormToJson("qForm"));
        layui.use('table', function () {
            var table = layui.table;
            t_table = layui.table;
            var laydate = layui.laydate;
            var cols = table.render({
                elem: '#list'
                , url: dUrl + conditon
                , height: 'full-280'   //固定高度-即固定表头固定第一行首行
                , skin: 'row' //表格风格
                , even: true
                , page: false
                , limits: [50, 100, 200]
                , limit: 50 //每页默认显示的数量
                , cols: [[
                      // { type: 'checkbox', fixed: 'left' }
                     { field: 'proCode', align: 'center', title: '存货编码', width: 130 }
                    , { field: 'proName', align: 'center', title: '存货名称', width: 150}
                    , { field: 'spec', align: 'center', title: '规格', width: 150}
                    , { field: 'unitName', align: 'center', title: '计量单位', width: 90 }
                    , { field: 'bzValue', align: 'center', title: '标准用量', width: 120 }
                    , { field: 'wareHouseName', align: 'center', title: '仓库', width: 130 }
                    , { field: 'number', align: 'center', title: '库存数', width: 100 }
                    , { field: 'zyNum', align: 'center', title: '占用数量', width: 100 }
                    , { field: 'kyNum', align: 'center', title: '可用数量', width: 100 }
                    , { field: 'memo', align: 'center', title: '采购数量', width: 100, edit: 'text' }
                    , { field: 'useMeter', align: 'center', title: '米数', width: 100, edit: 'text' }
                    , { field: 'useNum', align: 'center', title: '使用数量', width: 100, edit: 'text' }
                ]], done: function (data) {
                  
                }
            });

            //监听行工具事件
            table.on('edit(list)', function (obj) { 
                var data = obj.data;
                if ( Number(data.useNum) > Number(data.kyNum) ) {
                    layer.msg("对不起，占用数量已超过可用数量：" + data.proCode + "," + data.wareHouseName); 

                    data["useNum"] = "0";
                    $(this).val("0");
                    obj.update(data);

                }

                if (isNaN(obj.value)) {

                    layer.msg("输入格式错误");
                    data[obj.field] = "0";
                    
                }else {

                    //将采购数量存到临时表里
                    if (obj.field == "memo") {
                        addPurchase(obj.field, data);
                    }

                    //将米数存到临时表里
                    if (obj.field == "useMeter") {
                        addPurchase(obj.field, data);
                    }
                }
                    

            });


        });
    }

    //将采购数量存到临时表里
    function addPurchase(field,datas) {
        console.log(datas)
        $.ajax({
            url: "../Plan/addPurchaseList",
            data: { plCode: plCode, field: field, datas: JSON.stringify(datas) },
            type: "post",
            success: function (rsl) {
                if (rsl.result) {
                    loadPurchaseList()
                } else {
                    layer.msg(rsl.message);
                }
            }
        });
    }

    //获取添加的待采购清单
    function loadPurchaseList(){
        var conditon = JSON.stringify(FormToJson("qForm"));
        layui.use('table', function () {
            var table = layui.table;
            var laydate = layui.laydate;
            var cols = table.render({
                elem: '#cglist'
                , url: cgUrl + conditon
                , height: 'full-280'   //固定高度-即固定表头固定第一行首行
                , skin: 'row' //表格风格
                , even: true
                , page: true
                , limits: [50, 100, 200]
                , limit: 50 //每页默认显示的数量
                , cols: [[
                    // { type: 'checkbox', fixed: 'left' }
                    { field: 'proCode', align: 'center', title: '存货编码', width: 130 }
                    , { field: 'proName', align: 'center', title: '存货名称', width: 150 }
                    , { field: 'spec', align: 'center', title: '规格', width: 150 }
                    , { field: 'unitName', align: 'center', title: '计量单位', width: 90 }
                    , { field: 'bzValue', align: 'center', title: '标准用量', width: 120 }
                    , { field: 'kyNum', align: 'center', title: '可用数量', width: 100 }
                    , { field: 'memo', align: 'center', title: '采购数量', width: 100 }
                    , { field: 'meter', align: 'center', title: '米数', width: 100 }
                    , { field: 'right', align: 'center', title: '数据操作', toolbar: '#barDemo', width: 100, fixed: 'right' }
                ]], done: function (data) {

                }
            });

            
            //监听工具条
            table.on('tool(cglist)', function (obj) {
                var data = obj.data;
                if (obj.event == 'del') {
                    layer.confirm('确认要删除该数据？', function (index) {
                        var info = DelData(data.id);
                        // console.log(info)
                        if (info.result) {
                            layer.msg("删除成功");
                            obj.del();
                        } else {
                            layer.msg(info.message);
                        }
                        layer.close(index);
                    });
                }
            });
        });
    }

        /**
     删除采购清单
     **/
    function DelData(id) {
        var info = undefined;
        $.ajax({
            url: "../Plan/DelPurchaseList",
            data: { id: id },
            dataType: "json",
            type: "POST",
            async: false,
            success: function (data) {
                info = data;
            }
        });
        return info;
                
    }
                

    /**
    * 申请采购
    * **/
    function applyPurchase() {

        var tableData = new Array();  //bom列表数据
        var cDatas = new Array();   //采购数据

        layui.use(['table'], function () {
            var table = layui.table
            // tableData = table.cache['list']
            tableData = table.cache['cglist']
        });

        //console.log(tableData)

        if (tableData.length > 0) {
            for (var i = 0; i < tableData.length; i++) {

                // if (tableData[i]["memo"] != undefined) {
                //     //录入采购数量，即需要采购
                //     cDatas.push(tableData[i]);
                // }
                cDatas.push(tableData[i]);
            }

            //console.log(cDatas)
            if (cDatas.length > 0) {
                $.ajax({
                    url: "../Plan/SavePurchaseApply",
                    data: { plCode: plCode, cdatas: JSON.stringify(FormToJson("pForm")), datas: JSON.stringify(cDatas) },
                    type: "post",
                    success: function (rsl) {
                        if (rsl.result) {
                            layer.msg("已提交采购申请,请购单号："+rsl.data);
                        } else {
                            layer.msg(rsl.message);
                        }
                    }
                });
            } else {
                layer.msg("没有需要采购的存货");
                return;
            }



        } else {
            layer.msg("没有存货bom信息");
            return;
        }

    }



    layui.use(['multiSelect'], function () {
        var $ = layui.jquery, form = layui.form, multiSelect = layui.multiSelect;
    });
    /**
     * 获取多选的状态
     */
    function GetSelectMore() {
        var vals = [],
            texts = [];
        $('select[multiple] option:selected').each(function () {
            vals.push($(this).val());
            texts.push($(this).text());
        })
        $("input[ name='gxCodes']").val(vals.join(','));
        $("input[ name='gxNames']").val(texts.join(','));

    }

    /**
     * 确认占用
     * **/
    function confirmZy() {

        var tableData = new Array();  //bom列表库存数据
        var cDatas = new Array();   //占用数据

        layui.use(['table'], function () {
            var table = layui.table
            tableData = table.cache['list']
        });

        //console.log(tableData)


        GetSelectMore();
        var gxCodes = $("#gxCodes").val();
        var gxNames = $("#gxNames").val();

        if (gxCodes == "") {
            layer.msg("请选择占用工序"); return;
        }

        if (tableData.length > 0) {
            for (var i = 0; i < tableData.length; i++) {

                if (tableData[i]["useNum"] != undefined && tableData[i]["useNum"] !="0") {
                    //录入使用数量，即需要占用
                    cDatas.push(tableData[i]);
                }
            }

            //console.log(cDatas)
            if (cDatas.length > 0) {
                $.ajax({
                    url: "../Plan/SavePlanMeterial",
                    data: { plCode: plCode, numType: numType, cDatas: JSON.stringify(cDatas), gxCode: gxCodes, gxName: gxNames },
                    type: "post",
                    success: function (data) {
                        if (data.result) {
                            layer.alert("保存成功", { icon: 6 });
                            loadBOM();
                        } else {
                            layer.msg(data.message);
                        }
                    }
                });
            } else {
                layer.msg("您没有占用任何数据");
                return;
            }



        } else {
            layer.msg("没有存货bom信息");
            return;
        }

        
    }


</script>

